
//刷新购物车
function inicar() {
  var user_id = $("#user-id").val();
  $.ajax({
    url: "car.php",
    dataType: "json",
    data: {
      "user-id": user_id
    },
    success: function (data) {
      var html = ""
      console.log(data);
      for (var i = 0; i < data.length; i++) {        
          html += `<div class="car-list cle">
          <input type="hidden" id="pro-id" value="${data[i]["Id"]}">
            <img src="${data[i]["img_url"]}" alt="" class="fl">
            <div class="carp-list-t fl">
              <p class="pro-name">&nbsp;${data[i]["product"]}</p>
              <p>Dourthe Grands Terroirs Bordeaux Medium Sweet</p>
              <p><span class="dj">￥${data[i]["price"]}</span>×<span class="sl">${data[i]["number"]}</span></p>
            </div>
            <span class="gb">×</span>
            </div>`
          console.log("不是空车");      
      }
   
      if(data.length==0){
        html=`<div class="car0-pro">您的购物车里没有任何商品，赶快去挑选商品吧！</div>`
        console.log("空车");       
      }
      $(".carp-list-k").html(html);
      $(".carp-f-list-k").html(html);
      sumpro();
    }
  })
}
//购物车删除后数据库接口
function delcar(proid){

 
 
 $.ajax({
  url: "delcar.php",
  dataType: "json",
  data: {
    "proid": proid
  },
  success: function (data) {
    if(data){
      inicar()

      // car0()
    }
  }
})
}



//商品总数
function sumpro() {
  var n = 0;
  $(".carp-list-k .car-list").each(function () {
    n += Number($(this).find(".sl").text());
  })
  console.log("sumpro");

  $(".zl").html(n)
  $("#pro-n").html(n)
}
//删除商品
function del() {
  $(".carp-list-k").on("click", ".gb", function () {
    var proid=$(this).siblings("#pro-id").val()
    $(this).parent(".car-list").html("")
    sumpro()
    delcar(proid)
    
  })
  $(".carp-f-list-k").on("click", ".gb", function () {
    var proid=$(this).siblings("#pro-id").val()
    $(this).parent(".car-list").html("")
    sumpro()
    delcar(proid)
  })
}



inicar()
del()




//滑动淡出锚链接
$(window).scroll(function () {
  var top = $(window).scrollTop();
  if (top > 150) {
    $("#to-top").fadeIn()
  } else {
    $("#to-top").fadeOut()
  }
});
// 锚链接
$("#to-top").click(function () {
  $('html,body').animate({
    scrollTop: 0
  }, 500)
})

